/*
 * Copyright (c) 2021, Texas Instruments Incorporated
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * *  Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * *  Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * *  Neither the name of Texas Instruments Incorporated nor the names of
 *    its contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "ti_msp_dl_config.h"
#include "app.h"

// 编码器
ENCODER_RES encoder = {0};
// 巡线传感器
TrackerData tracker = {0};
// 按键
KeyData key = {0};
// 串口调试
char msg[50] = {0};

int main(void)
{
    SYSCFG_DL_init();

    // Peripheral initialization code here
    Uart_Debug_NVIC_init();
    encoder_init();
    Set_Motor_PWM(0, 0);
    analog_tracker_init();
    sys_init();
    Interrupt_Init();
    SysTick_Init();
    OLED_Init();

    menu_init();
    app_init();
    while (1)
    {
        if (get_sys_update_flag())
        {
            // 获取编码器速度
            encoder_update();
            get_encoder_counter_data(&encoder);
            encoder_reset();

            // 获取巡线传感器数值
            update_tracker_values();
            get_analog_tracker_values(&tracker);
            get_digital_tracker_values(&tracker);

            // OLED显示模拟量
            // sprintf(msg, "T:%03d", tracker_data.threshold);
            // OLED_ShowString(0, 5, (uint8_t *)msg, 8);
            // sprintf(msg, "DT:%03d", tracker_data.delta_threshold);
            // OLED_ShowString(0, 6, (uint8_t *)msg, 8);
            // sprintf(msg, "%03d %03d %03d %03d", tracker.analog_sensors[0], tracker.analog_sensors[1], tracker.analog_sensors[2], tracker.analog_sensors[3]);
            // OLED_ShowString(0, 3, (uint8_t *)msg, 8);
            // sprintf(msg, "%03d %03d %03d %03d", tracker.analog_sensors[4], tracker.analog_sensors[5], tracker.analog_sensors[6], tracker.analog_sensors[7]);
            // OLED_ShowString(0, 4, (uint8_t *)msg, 8);


            // OLED显示数字量
            // sprintf(msg, "D:%d%d%d%d%d%d%d%d", tracker.digital_sensors[0], tracker.digital_sensors[1], tracker.digital_sensors[2], tracker.digital_sensors[3], tracker.digital_sensors[4], tracker.digital_sensors[5], tracker.digital_sensors[6], tracker.digital_sensors[7]);
            // // Uart_Debug_transmitString(msg);
            // OLED_ShowString(0, 7, (uint8_t *)msg, 8);

            // 获取按键状态
            get_key_data(&key);
            // 处理按键事件
            parse_menu_command(key.key0);
            set_sys_target_conner(get_menu_rounds() * 4 + 1); // 设置目标转弯次数
            switch_sys_running_state(key.key1);
            // 重置按键状态
            reset_keys_data();

            // 运行主程序
            if (get_sys_running_state() == RUNNING)
            {
                // sprintf(msg, "running!    \r\n");
                // Uart_Debug_transmitString(msg);
                // 小车运行中，角点小于目标角点，继续巡线
                if (get_sys_current_conner() < get_sys_target_conner())
                {
                    // 需要转弯
                    // if (tracker.digital_sensors[0] == 1)
                    // {
                    //     // 左转，堵塞
                    //     set_sys_current_conner(get_sys_current_conner() + 1);
                    //     Set_Motor_PWM(-200, 200);
                    //     delay_ms(500);
                    //     sprintf(msg, "left!    \r\n");
                    //     Uart_Debug_transmitString(msg);
                    // }
                    // if (tracker.digital_sensors[7] == 1)
                    // {
                    //     // 右转，堵塞
                    //     set_sys_current_conner(get_sys_current_conner() + 1);
                    //     Set_Motor_PWM(200, -200);
                    //     delay_ms(500);
                    //     sprintf(msg, "right!    \r\n");
                    //     Uart_Debug_transmitString(msg);
                    // }
                    car_track_line(tracker.digital_sensors);
                    // 如果tracker.digital_sensors全为
                }
                else
                {
                    // 串口发送停止信息
                    // sprintf(msg, "stop!    \r\n");
                    // Uart_Debug_transmitString(msg);
                    // 达到目标转弯次数，停止
                    Set_Motor_PWM(0, 0);
                    set_sys_current_conner(0);
                    switch_sys_running_state(1); // 切换到IDLE状态
                }
            }
            else if (get_sys_running_state() == IDLE)
            {
                // sprintf(msg, "idle!    \r\n");
                // Uart_Debug_transmitString(msg);
                Set_Motor_PWM(0, 0);
                set_sys_current_conner(0);
            }
            set_sys_update_flag(false);
        }
    }
}